import presetVar from './presetVar';

/**
 * @description 根据收款对象设置客户\供应商可修改性
 */
const PAYTARGET = {
    YUANGONG: 0,
    GONGYS: 1,
    KEHU: 2
}
 function setFormEditableKeys(props) {
    let paytarget = props.form.getFormItemsValue("head", ['paytarget']) == null ? "" : props.form.getFormItemsValue("head", ['paytarget'])[0].value//收款对象
    if(props.form.getFormItemsVisible('head', 'paytarget')) {
        if (paytarget == PAYTARGET.YUANGONG) {
            props.form.setFormItemsDisabled("head", { 'hbbm': true, 'customer': true, 'hbbm_v': true, 'customer_v': true, "custaccount": true, 'receiver': false, 'custaccount': true, 'freecust': true });
        } else if (paytarget == PAYTARGET.GONGYS) {
            props.form.setFormItemsDisabled("head", { 'receiver': true, 'customer': true, 'hbbm': false, 'customer_v': true, 'hbbm_v': false, 'custaccount': false, 'freecust': false });
        } else if (paytarget == PAYTARGET.KEHU) {
            props.form.setFormItemsDisabled("head", { 'hbbm': true, 'hbbm_v': true, 'receiver': true, 'customer': false, 'customer_v': false,'custaccount': false, 'freecust': true });
        }
    }
}

function setAllCardTableEditableKeys(props, data={}) {
        let validBodyArray = [];
        Object.values({body1: "er_cshare_detail"}).forEach((one)=>{
            if (data.bodys[one]){
                let temp = {
                    "tablecode" : one,
                    "tabledata" : data.bodys[one]
                }
                validBodyArray.push(temp);
            }
        })

        //拉单生成的报销单明细需要控制收款对象相关字段的可编辑性
        validBodyArray.forEach(validBody => {
            let tablecode = validBody.tablecode;
            let rows = props.cardTable.getAllRows(tablecode)
            if (rows){
                rows.forEach(row => {
                    let currentrow_paytarget = props.form.getFormItemsValue("head", ['paytarget']) == null ? "" : props.form.getFormItemsValue("head", ['paytarget'])[0]//收款对象
                    if (currentrow_paytarget && props.form.getFormItemsVisible('head', 'paytarget')){
                        let currentrow_paytargetvalue = currentrow_paytarget.value
                        //控制表体收款对象相关字段的可编辑性
                        let editablekeys = []
                        let uneditablekeys = []
                        if ("0" == currentrow_paytargetvalue){
                            editablekeys = ['receiver','skyhzh']
                            uneditablekeys = ['hbbm','hbbm_v','customer','customer_v','custaccount']
                        }else if("1" == currentrow_paytargetvalue){
                            editablekeys = ['hbbm','hbbm_v','custaccount']
                            uneditablekeys = ['receiver','skyhzh','customer','customer_v']
                        }else if("2" == currentrow_paytargetvalue){
                            editablekeys = ['customer','customer_v','custaccount']
                            uneditablekeys = ['receiver','skyhzh','hbbm','hbbm_v']
                        }
                        props.cardTable.setEditableByRowId(tablecode, row.rowid, editablekeys, true)
                        props.cardTable.setEditableByRowId(tablecode, row.rowid, uneditablekeys, false)
                    }
                    // 表体数据初始化时需设置汇率相关字段可编辑性
                    let currentrow_bzbm = props.cardTable.getValByKeyAndRowId(tablecode, row.rowid, 'bzbm')
                    console.log('ddd',currentrow_bzbm)
                    let rate_category = props.cardTable.getValByKeyAndRowId(tablecode, row.rowid, 'zzhllx.rate_category')
                    zzhllxLogic(currentrow_bzbm,rate_category,'body',props,'bbhl',tablecode,row.rowid)
                })
            }
            
        })
}

/**
 * 
 * @param {*} rate_category 当前汇率类型
 * @param {*} windowTypes 所有汇率类型
 * @returns Y：五种类型之一，N：非五种类型，-1：空
 */
 function getZzhllxValue(rate_category = '',windowTypes){
    if(rate_category.display){
        let curType = windowTypes.find((item) => {
            return item.display === rate_category.display
        })
        let value = curType.value
        if(value == '0' || value == '1' || value == '2' || value == '3' || value == '4') return 'Y'
        return 'N'
    } else {
        return -1
    }
}

/**
 * 汇率适配逻辑
 * @param {*} currentrow_bzbm:当前币种，
 * @param {*} rate_category：汇率类型，
 * @param {*} type: 'head',表单，'body‘，表体，
 * @param {*} props:props
 * @param {*} moduleId:当前页签
 * @param {*} rowid:当前行
 * @returns
 */
 function zzhllxLogic(currentrow_bzbm,rate_category,type,props,bbhl = 'bbhl',moduleId,rowid){
    //  借款单中本币汇率字段：是org_currinfo不是bbhl，共用方法中 使用org_currinfo为本币汇率
    let zzbwb = window.presetVar.zzbwb //组织币种
    let ratecontorl = window.presetVar.ratecontorl   // 集团参数NC004 N 不允许修改，Y 允许修改
    let zzhllxValue = getZzhllxValue(rate_category,window.presetVar.zzhllxs)  // Y:五种中的一种，N:非五种，-1：空
    let editKeys = []
    let unEditKeys = []
    if(currentrow_bzbm && currentrow_bzbm.value == zzbwb){
        unEditKeys = [bbhl,'zzhllx']
        editKeys = ['zzhllyrq']
        if(zzhllxValue < 0){
            unEditKeys = ['zzhllyrq',bbhl,'zzhllx']
            editKeys = []
            type == 'head' ? props.form.setFormItemsValue(presetVar.head.head1,{'zzhllyrq':{display: null, value: null}}) : props.cardTable.setValByKeyAndRowId(moduleId, rowid, 'zzhllyrq',{value:null, display: null}) //清空组织汇率来源日期
        }
    } else if( zzhllxValue =='Y' && ratecontorl == 'N'){  //缺少对应5种组织汇率类型及集团参数判断
        unEditKeys = [bbhl]
        editKeys = ['zzhllyrq','zzhllx']
    }  else if(zzhllxValue < 0){
        unEditKeys = ['zzhllyrq']
        editKeys = [bbhl,'zzhllx']
        type == 'head' ? props.form.setFormItemsValue(presetVar.head.head1,{'zzhllyrq':{display: null, value: null}}) : props.cardTable.setValByKeyAndRowId(moduleId, rowid, 'zzhllyrq',{value:null, display: null}) //清空组织汇率来源日期
    } else{
        unEditKeys = []
        editKeys = [bbhl,'zzhllyrq','zzhllx']
    }
    if(type == 'head'){
        editKeys.forEach((item) => {
            props.form.setFormItemsDisabled('head',{[item]: false});
        })
        unEditKeys.forEach((item) => {
            props.form.setFormItemsDisabled('head',{[item]: true});
        })
    } else{
        props.cardTable.setEditableByRowId(moduleId, rowid, editKeys, true)
        props.cardTable.setEditableByRowId(moduleId, rowid, unEditKeys, false)
    }
     
}

export {
    setFormEditableKeys,
    setAllCardTableEditableKeys,
    getZzhllxValue,
    zzhllxLogic
}